<?php

class MyController extends BaseController
{

    private $req;
    public function init()
    {
        $this->req = $this->getRequest();
        // 全局搜索专用
        $this->view->breadcrumb = array(array(
            'url' => '/index',
            'name' => '设置',
        ));
    }
    /**
     * 修改密码
     */
    public function indexAction()
    {
        $login = new Zend_Session_Namespace('login');
        $id = $login->user_id;//登录员工账号ID
        if (empty($id)) {
            return Response::Error(0, '系统错误');
        }
        try {
            // 检查请求
            if ($this->req->isPost()) {
                $password_old = (string)$this->req->getPost('password_old');
                $password_new = (string)$this->req->getPost('password_new');
                $password_again = (string)$this->req->getPost('password_again');

                if (empty($password_old)) {
                    return Response::Error(0, '旧密码不能为空');
                }

                if (empty($password_new)) {
                    return Response::Error(0, '新密码不能为空');
                }

                if (empty($password_again)) {
                    return Response::Error(0, '确认密码不能为空');
                }

                if ($password_new !== $password_again) {
                    return Response::Error(0, '新密码和确认密码不相同');
                }
                $staffBiz = new \Biz\Staff();
                $staff_cls = $staffBiz->getById($id);
                if(empty($staff_cls)){
                    return Response::Error(0, '员工信息不存在');
                }
                $md5_password_old = md5($password_old);
                $md5_password_db = $staff_cls['pwd'];
                if ($md5_password_db !== $md5_password_old) {
                    return Response::Error(0, '原密码不正确');
                }
                $md5_password_new = md5($password_new);
                $staff_cls['pwd']=$md5_password_new;

                if ($staffBiz::update($staff_cls) == true) {
                    return Response::Success('', '修改成功',200);
                }
                return Response::Error(0, '修改失败');
            }
        } catch (Exception $e) {
            return Response::Error($e->getCode(), $e->getMessage());
        }
        $this->view->breadcrumb[] = array(
            'name' => '修改密码'
        );
    }
}